using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class HandleTipsWindow : MonoBehaviour
{
    public Image tipsBg;
    public TextMeshProUGUI tipText;
    public Animator ani;
    
    //定义一个队列，存放所有要显示的tips
    private Queue<string> tipsQueue = new Queue<string>();
    private bool isTipsShow = false;//是否正在显示tips
    
    public void AddTip(string tips)
    {
        tipsQueue.Enqueue(tips);    
    }
    
    private void Update()
    {
        if (tipsQueue.Count > 0 && !isTipsShow)
        {
            string tips = tipsQueue.Dequeue();
            isTipsShow = true;
            ShowTip(tips);
        }       
    }
    
    private void ShowTip(string tips)
    {
        tipsBg.gameObject.SetActive(true);
        //根据tips的长度设置tipImage的大小
        tipsBg.rectTransform.sizeDelta = new Vector2(tips.Length * 30 + 80, 100);
        tipText.text = tips;
        //播放动画
        // Debug.Log(ani);
        ani.Play("TipsWindowAni",0,0);
        
        //判断动画播放时间
        float time = ani.GetCurrentAnimatorStateInfo(0).length;
        // Debug.Log("动画播放时间: " + time);
        
        //动画播放完毕后，自动隐藏
        StartCoroutine(HideTip(time));
    }
    
    IEnumerator HideTip(float seconds = 0.1f)
    {
        yield return new WaitForSeconds(seconds);
        tipsBg.gameObject.SetActive(false);
        isTipsShow = false;
    }
}
